var appPath = getAppPath();	

$(function(){
	initJspGrid();
});


function initJspGrid(){
	$('#priceGrid').datagrid({
	    url:appPath + "/yjxForm/getFormMgrInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
		checkbox:false,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'form_id',title:'表单ID',width:25,align:'center',sortable:false}, 
	        {field:'form_type',title:'表单类型',width:35,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = 'Nan';
	        		switch(value){
	        		case 10: ret = '用户收集'; break;
	        		}
	        		return '<span style="color:#B22222;">'+ret+'</span>';
	        	}
	        },
	        {field:'key1',title:'项目1',width:25,align:'center',sortable:true},
	        {field:'key2',title:'项目2',width:25,align:'center',sortable:true},
	        {field:'key3',title:'项目3',width:25,align:'center',sortable:true},
	        {field:'key4',title:'项目4',width:25,align:'center',sortable:true},
	        {field:'key5',title:'项目5',width:25,align:'center',sortable:true},
	        {field:'key6',title:'项目6',width:25,align:'center',sortable:true},
	        {field:'key7',title:'项目7',width:25,align:'center',sortable:true},
	        {field:'key8',title:'项目8',width:25,align:'center',sortable:true},
	        {field:'key9',title:'项目9',width:25,align:'center',sortable:true},
	        {field:'key10',title:'项目10',width:25,align:'center',sortable:true},
	        {field:'desc',title:'描述',width:40,align:'center',sortable:true},
	        {field:'thumb',title:'缩略图',width:40,align:'center',sortable:true},
	        {field : 'edit',title : '编辑',width : 18,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return '<a href="javascript:void(0)" style="font-size:14px;" onclick="showDetail('+rowData.form_id+')">编辑</a>';
				}
			},
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:true},
	        {field:'update_time',title:'修改时间',width:60,align:'center',sortable:true}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[
		{
			text:'添加表单',
			iconCls:'icon-add',
			handler:function(){
				addFormInfo();
			}
		},
		{
			text:'删除表单',
			iconCls:'icon-no',
			handler:function(){
				delFormInfo();
			}
		}]
	    
	});

}

/* 
 * 展示单个信息详情 用于修改
 */
function showDetail(id) {
	$('#editForm').form('clear');
	$("#editTable").show();
	$("#formId").val(id);
	
	var url = appPath + "/yjxForm/getOneFormMgrInfo.action?formId="+id
	$.get(url,function(ret) {
		if (ret && ret.length > 0) {
				var info = ret[0];
				var editDesc = $("#editDesc").val(info.desc);
				var editName1 = $("#editName1").val(info.key1);
				var editName2 = $("#editName2").val(info.key2);
				var editName3 = $("#editName3").val(info.key3);
				var editName4 = $("#editName4").val(info.key4);
				var editName5 = $("#editName5").val(info.key5);
				var editName6 = $("#editName6").val(info.key6);
				var editName7 = $("#editName7").val(info.key7);
				var editName8 = $("#editName8").val(info.key8);
				var editName9 = $("#editName9").val(info.key9);
				var editName10 = $("#editName10").val(info.key10);
				$("#editFormType").combobox({
					data:[{'id':10,'name':'用户作品收集'}],
					valueField:'id',
					textField:'name',
					value:info.form_type
				});
			}
		});
		$("#editBtn").show();
		$('#editDlg').window({
			title : '表单编辑页面',
			iconCls : 'icon-edit',
			modal : true,
			shadow : true,
			collapsible : false,
			minimizable : false,
			maximizable : false
		});
	
		$('#editDlg').window('move', {
			top : 20,
			left : 20
		});
		$('#editDlg').window('open');
}



function delFormInfo(){
	var selectItem=$("#priceGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','选择需要的删除的表单','warning');
		return;
	}
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) { 
			var ids = "";
			$.each(selectItem, function(index, item){
				ids = ids + item.form_id + ",";
			});
			console.log(ids);
			var url = appPath+"/yjxForm/delFormMgr.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','删除成功','info');
					doQuery();
				} else {
					$.messager.alert('提示信息','删除失败','warning');
				}
			});
        }
	});
}


function getQueryParam(){
	var queryObject = {};
	var queryFormType = $("#queryFormType").combobox('getValue');
	if(queryFormType&&queryFormType.length>0) queryObject.findFormType = queryFormType;
	return queryObject;
}

/*  查询数据
 */
function doQuery(){
	$("#priceGrid").datagrid('unselectAll');
	$("#priceGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

// 新建价格信息
function addFormInfo(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$('#editDlg').window({
		title : '新建表单内容',
		iconCls : 'icon-edit',
		modal : true,
		shadow : true,
		collapsible : false,
		minimizable : false,
		maximizable : false
	});
	$("#editPrices").attr('disabled','disabled'); // 区间计价默认为不可选
	$("#editFormType").combobox({
		data:[{'id':10,'name':'用户作品收集'}],
		valueField:'id',
		textField:'name',
		value:10
	});
	$('#editDlg').window('move', {
		top : 20,
		left : 20
	});
	$('#editDlg').window('open');
}

//确认提交编辑
function doEdit() {
	var editDesc = $("#editDesc").val();
	var editName1 = $("#editName1").val();
	var editName2 = $("#editName2").val();
	var editName3 = $("#editName3").val();
	var editName4 = $("#editName4").val();
	var editName5 = $("#editName5").val();
	var editName6 = $("#editName6").val();
	var editName7 = $("#editName7").val();
	var editName8 = $("#editName8").val();
	var editName9 = $("#editName9").val();
	var editName10 = $("#editName10").val();
	var formId = $("#formId").val();
	
	var editFormType = $("#editFormType").combobox('getValue');
	
	if (editFormType) {
		var	 url = appPath + "/yjxForm/editFormMgrInfo.action";
		var sendData = {
				"formMgrDto.form_id" : formId || "",
				"formMgrDto.form_type" : editFormType || "",
				"formMgrDto.key1" : editName1 || "",
				"formMgrDto.key2" : editName2 || "",
				"formMgrDto.key3" : editName3 || "",
				"formMgrDto.key4" : editName4 || "",
				"formMgrDto.key5" : editName5 || "",
				"formMgrDto.key6" : editName6 || "",
				"formMgrDto.key7" : editName7 || "",
				"formMgrDto.key8" : editName8 || "",
				"formMgrDto.key9" : editName9 || "",
				"formMgrDto.key10" : editName10 || "",
				"formMgrDto.desc" : editDesc || ""
			};
			$.post(url, sendData, function(data) {
				if (data == "ok") {
					$.messager.alert('提示信息', '提交成功', 'info');
					$('#editDlg').window('close');
					doQuery();
				}else {
					$.messager.alert("提示信息", "提交失败", 'info');
				}
			});	
	}else{
		$.messager.alert('提示信息', '表单类型不能为空', 'info');
	}
}

//取消
function cancelEdit(){
	$('#editDlg').window('close');
	$("#editForm").form('clear');
}
